<?php
include_once ('class.mysql.php');

class RegisterOwner
{
    private $comtec_id_mainteiner;
    private $cod_owner;
    private $company_name;
    private $rut;
    private $address;
    private $email;
    private $phone; 
    private $token;
    private $password;
    private $contact_name;
    private $contact_lastname1;
    private $contact_lastname2;
    private $contact_position;
    
    private $status_reg;   
    
    private $errors;
    
    public function __construct()
    {
        $this->errors   = array();
	
        $this->comtec_id_mainteiner = $_POST['comtec_id_mainteiner'];
	$this->cod_owner	= $_POST['cod_owner'];
	$this->company_name	= $_POST['company_name'];
	$this->rut		= $_POST['rut'].$_POST['rut_cod'];
	$this->address		= $_POST['address'];
	$this->email		= strtolower($_POST['email']);
	$this->phone		= $_POST['phone'];
        $this->password         = $_POST['password'];
        
        $this->contact_name      = $_POST['contact_name'];
        $this->contact_lastname1 = $_POST['contact_lastname1'];
        $this->contact_lastname2 = $_POST['contact_lastname2'];
        $this->contact_position  = $_POST['contact_position'];
        
	$this->status_reg	= 1;
        $this->token		= $_POST['token'];
    }

    public function process()
    {
        if($this->valid_token() && $this->valid_data())
            $this->register();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function register()
    {
     $db = new MySQL();
        
     $passwmd5 = md5($this->password);
          
     $result = $db->query("INSERT INTO users (
                            comtec_id_mainteiner,                                 
                            phone,
                            email,
                            passwmd5,
                            access_level,
                            name,
                            lastname1,
                            lastname2,
                            position,
                            status_reg
                           ) VALUES (
                                $this->comtec_id_mainteiner,
                                '$this->phone',
                                '$this->email',
                                '$passwmd5',
                                5,
                                '$this->contact_name',
                                '$this->contact_lastname1',
                                '$this->contact_lastname2',
                                '$this->contact_position',
                                1
                               )");
          
        $result =  $db->fetch_array($db->query("SELECT comtec_id_user FROM users WHERE email = '$this->email'; "));
        $comtec_id_user = $result['comtec_id_user'];
              
        $stmt = $db->query("INSERT INTO owner (    
	    comtec_id_mainteiner,
            cod_owner,
	    company_name,
	    rut,
	    address,
	    email,
	    phone,
            contact_name,
            contact_lastname1,
            contact_lastname2,
            contact_position,
            comtec_id_user,  
	    status_reg
	) VALUES (
            $this->comtec_id_mainteiner,    
            '$this->cod_owner',
            '$this->company_name',
            '$this->rut',
            '$this->address',
	    '$this->email',
	    '$this->phone',
            '$this->contact_name',   
            '$this->contact_lastname1',   
            '$this->contact_lastname2',
            '$this->contact_position',
             $comtec_id_user,      
             $this->status_reg
        )");

        if($db->affected_rows() < 1)
            $this->errors[] = 'No se puede procesar la inscripci&oacute;n';  
    }

    public function user_exists()
    {
     $db= new MySQL();
     $stmt = $db->query("SELECT comtec_id_owner FROM owner WHERE email = '$this->email'");
     return $db->num_rows($stmt)? 1 : 0;
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data()
    {
        if($this->user_exists())
            $this->errors[] = 'Due&ntilde;o ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
